<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %><!-- 数据库操作需要 -->        
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
	String id=request.getParameter("id");
	String password=request.getParameter("password");
	String department=request.getParameter("department");
	String ID="";
	System.out.println(id+" "+password+" "+department);
//加载数据库驱动程序，可能会引发找不到驱动类的异常
		try {
			Class.forName("org.h2.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return;
		}
		String url="jdbc:h2:d:/CompanyCar/temp/mydbtest";
		Connection conn=null;
		
		//数据库连接与查询过程中可能会引发sql异常
		try {
			//获取数据库连接(url,username,password)			
			conn = DriverManager.getConnection(url,"sa","");
		/*	
			//获取表达式对象，准备执行sql语句,该sqlsql语句有两个输入参数
			String sql="update test set name=? where id=?";			
			PreparedStatement pstmt=conn.prepareStatement(sql);
			
			//设置sql参数
			pstmt.setString(1, "Li Ming");	//参数为字符串类型
			pstmt.setInt(2, 2);		//参数为int类型
			
			//执行数据库更新
			pstmt.executeUpdate();
			
			//关闭表达式对象
			pstmt.close();	
		*/	
			//准备执行数据库查询
			String sql="";
			if("司机".equals(department))
				sql="select Did from Driver where Did='"+id+"' and Dpassword='"+password+"' ";
			else if("职员".equals(department))
				sql="select EmpID from Employee where EmpID='"+id+"' and password='"+password+"'  ";
			else 
				sql="select id from Admin where id='"+id+"' and password='"+password+"' and department='"+department+"' ";
			
			Statement stmt=conn.createStatement();
			
			//pstmt.setString(1, account);
			//执行数据库查询，获取结果集
			ResultSet rs=stmt.executeQuery(sql);
			
			//循环获取结果集当中的所有数据
			if (rs.next()){
				ID=rs.getString(1);	//获取第1个字段
			}				
			//关闭表达式对象
			stmt.close();			
			//关闭数据库			
			conn.close();			
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally{
			//防止数据库未正常关闭
			try{
				if (conn!=null && (!conn.isClosed())){
					conn.close();
				}
			}catch(SQLException e){
				e.printStackTrace();
			}
		}


if(ID==""){
	System.out.println("nobody");
	request.setAttribute("loginok","0");
	request.setAttribute("lastID",id);
	request.setAttribute("lastpassword",password);
	request.getRequestDispatcher("/C-form_login.jsp").forward(request,response);//查找不到对应的姓名则返回起始页
}
else{
	
	if ("管理端".equals(department))
	{		
		session.setAttribute("ID", id);
	%>
		 <script type="text/javascript">
			 window.parent.location.href = "C-Admin.jsp";
    	</script>
	<%
	}else if("财务部".equals(department))
	{		
		session.setAttribute("ID", id);
	%>
		<script type="text/javascript">
			 window.parent.location.href = "G-Finance.jsp";
    	</script>
	<%
	 
	}else if("运输端".equals(department))
	{
		session.setAttribute("ID", id);
	%>
		<script type="text/javascript">
			 window.parent.location.href = "D-car_dispatch.jsp";
    	</script>
	<%
	}else if("司机".equals(department))
	{
		session.setAttribute("Did", id);
	%>
		<script type="text/javascript">
			 
			 window.parent.location.href = "X-Driver_login.jsp";
    	</script>
	<%
	}
	else 
	{
		session.setAttribute("EmpID", id);
	%>
		<script type="text/javascript">
			
			 window.parent.location.href = "X-UseCar_login.jsp";
    	</script>
	<%
		
	}
}
%>
</body>
</html>